package org.uam.util;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.util.Assert;

//Referenced classes of package com.huawei.bme.service.common:
//         Constant

public class DateAS
{

 public DateAS()
 {
 }

 public static boolean isValidDate(int year, int month, int day)
 {
     if(month > 0 && month <= 12 && day > 0 && day <= daysPerMonth[month - 1])
         return true;
     if(month == 2 && day == 29)
         return isLeapYear(year);
     else
         return false;
 }

 public static boolean isValidDateTime(int year, int month, int day, int hour, int minute, int second)
 {
     if(month > 0 && month <= 12 && day > 0 && day <= daysPerMonth[month - 1] && hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 60)
         return true;
     if(month == 2 && day == 29)
         return isLeapYear(year);
     else
         return false;
 }

 public static boolean isLeapYear(int year)
 {
     return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
 }

 public static String getCurrentTime(String format)
 {
     return convertDate2String(new Date(), format);
 }

 public static Date getDefaultExpireTime()
 {
     return defaultExpireTime;
 }

 public static Timestamp getDefaultExpireTimestamp()
 {
     return convert2SQLTimestamp(defaultExpireTime);
 }

 public static boolean isIntraday(Date compareDate, Date todayTime)
 {
     String dateString1 = convertDate2String(compareDate, "yyyyMMdd");
     String dateString2 = convertDate2String(todayTime, "yyyyMMdd");
     return compareTime(dateString1, dateString2, "yyyyMMdd") != 1;
 }

 private static Date modify(Date date, int calendarField, int amount)
 {
     if(date == null)
     {
         throw new IllegalArgumentException("The date must not be null");
     } else
     {
         Calendar c = Calendar.getInstance();
         c.setTime(date);
         c.add(calendarField, amount);
         return c.getTime();
     }
 }

 public static boolean isBeforeDefaultExpireTime(Date date)
 {
     return date.before(getDefaultExpireTime());
 }

 public static int compareTime(String time1, String time2, String format)
 {
     Date date1 = convertString2Date(time1, format);
     Date date2 = convertString2Date(time2, format);
     return date1.compareTo(date2);
 }

 public static boolean isCurrentBeforeTime(Date date)
 {
     return (new Date()).compareTo(date) == -1;
 }

 public static boolean isCurrentAfterTime(Date date)
 {
     return (new Date()).compareTo(date) == 1;
 }

 public static Date convertString2Date(String dateString, String format)
 {
     if(null == dateString || dateString.trim().length() == 0)
         return null;
     if(null == format || format.trim().length() == 0)
         format = "yyyyMMddHHmmss";
     SimpleDateFormat dataFormat = new SimpleDateFormat(format);
     Date date = null;
     try
     {
         date = dataFormat.parse(dateString.substring(0, format.length()));
     }
     catch(ParseException e)
     {
//         throw new GeneralBMEException(e);
     }
     catch(Exception e)
     {
//         throw new GeneralBMEException(e);
     }
     return date;
 }

 public static Timestamp convertString2Timestamp(String dateString, String format)
 {
     if(null == dateString || dateString.trim().length() == 0)
         return null;
     if(null == format || format.trim().length() == 0)
         format = "yyyyMMddHHmmss";
     SimpleDateFormat dataFormat = new SimpleDateFormat(format);
     Timestamp timestamp = null;
     try
     {
         Date date = dataFormat.parse(dateString.substring(0, format.length()));
         timestamp = new Timestamp(date.getTime());
     }
     catch(ParseException e)
     {
//         throw new GeneralBMEException(e);
     }
     catch(Exception e)
     {
//         throw new GeneralBMEException(e);
     }
     return timestamp;
 }

 public static String convertDate2String(Date date, String format)
 {
     if(null == date)
         return null;
     if(null == format || format.trim().length() == 0)
         format = "yyyyMMddHHmmss";
     FastDateFormat fdf = (FastDateFormat)fastFormatCache.get(format);
     if(fdf == null)
     {
         fdf = FastDateFormat.getInstance(format);
         fastFormatCache.put(format, fdf);
     }
     return fdf.format(date);
 }

 public static String convertDate2StringWithQuot(Date date, String format)
 {
     return (new StringBuilder("'")).append(convertDate2String(date, format)).append("'").toString();
 }

 public static Date getCurrentTime()
 {
     return new Date();
 }

 public static Date getDefaultApplyTime(String format)
 {
     return convertString2Date(getCurrentTime(format), format);
 }

 public static java.sql.Date getCurrentSQLDate()
 {
     java.sql.Date current = new java.sql.Date(System.currentTimeMillis());
     return current;
 }

 public static Time getCurrentSQLTime()
 {
     Time current = new Time(System.currentTimeMillis());
     return current;
 }

 public static Timestamp getCurrentSQLTimestamp()
 {
     Date applyTime = getDefaultApplyTime("yyyyMMddHHmmss");
     Timestamp current = convert2SQLTimestamp(applyTime);
     return current;
 }

 public static String getCurrentDateTimeStr()
 {
     return convertDate2String(getCurrentTime(), "yyyyMMddHHmmss");
 }

 public static java.sql.Date convert2SQLDate(Date date)
 {
     if(date == null)
     {
         return null;
     } else
     {
         java.sql.Date date2 = new java.sql.Date(date.getTime());
         return date2;
     }
 }

 public static Time convert2SQLTime(Date date)
 {
     if(date == null)
     {
         return null;
     } else
     {
         Time date2 = new Time(date.getTime());
         return date2;
     }
 }

 public static Timestamp convert2SQLTimestamp(Date date)
 {
     if(date == null)
     {
         return null;
     } else
     {
         Timestamp date2 = new Timestamp(date.getTime());
         return date2;
     }
 }

 public static Date convertTimestamp2Date(Timestamp date)
 {
     if(date == null)
     {
         return null;
     } else
     {
         Date date2 = new Date(date.getTime());
         return date2;
     }
 }

 public static boolean isBetween(Timestamp start, Timestamp current, Timestamp end)
 {
     boolean checkStart = true;
     boolean checkEnd = true;
     if(start != null)
         checkStart = start.before(current);
     if(end != null)
         checkEnd = end.after(current);
     return checkStart && checkEnd;
 }

 public static boolean isCurrentBetween(Timestamp start, Timestamp end)
 {
     Timestamp current = getCurrentSQLTimestamp();
     return isBetween(start, current, end);
 }

 public static boolean isCurrentBetween(Date start, Date end)
 {
     return isCurrentBetween(convert2SQLTimestamp(start), convert2SQLTimestamp(end));
 }

 public static long intervalTime(Date oldDate, Date newDate, int type)
 {
     long day = 86400000L;
     if(type == 0)
         return (newDate.getTime() - oldDate.getTime()) / day;
     else
         return (newDate.getTime() - oldDate.getTime()) / (day * 30L);
 }

 public static Date modifyYears(Date date, int amount)
 {
     return modify(date, 1, amount);
 }

 public static Date modifyMonths(Date date, int amount)
 {
     return modify(date, 2, amount);
 }

 public static Date modifyWeeks(Date date, int amount)
 {
     return modify(date, 3, amount);
 }

 public static Date modifyDays(Date date, int amount)
 {
     return modify(date, 5, amount);
 }

 public static Date modifyHours(Date date, int amount)
 {
     return modify(date, 11, amount);
 }

 public static Date modifyMinutes(Date date, int amount)
 {
     return modify(date, 12, amount);
 }

 public static Date modifySeconds(Date date, int amount)
 {
     return modify(date, 13, amount);
 }

 public static Date modifyMilliseconds(Date date, int amount)
 {
     return modify(date, 14, amount);
 }

 public static Date getDateWithZero(Date oriTime, int nums, int field)
 {
     Assert.notNull(oriTime);
     Date date = cleanDateToZero(oriTime);
     Calendar calendar = new GregorianCalendar();
     calendar.setTime(date);
     calendar.add(field, nums);
     return calendar.getTime();
 }

 public static Date cleanDateToZero(Date date)
 {
     Assert.notNull(date);
     String dateString = convertDate2String(date, "yyyyMMdd");
     dateString = (new StringBuilder()).append(dateString).append("000000").toString();
     return convertString2Date(dateString, "yyyyMMddHHmmss");
 }

 public static boolean isAfterDay(Date date1, Date date2)
 {
     Assert.notNull(date1);
     Assert.notNull(date2);
     Date date_1 = cleanDateToZero(date1);
     Date date_2 = cleanDateToZero(date2);
     return date_1.after(date_2);
 }

 public static Long getTime(Date date)
 {
     Long timeLongValue = null;
     if(date != null)
         timeLongValue = Long.valueOf(date.getTime());
     return timeLongValue;
 }

 public static String formateDBDate(String dateStr)
 {
     StringBuilder sb = new StringBuilder();
     sb.append(TO_DATE);
     sb.append("('");
     sb.append(dateStr);
     sb.append("','");
     sb.append(DEFAULT_DB_DATE_FMT);
     sb.append("')");
     return sb.toString();
 }

 public static String getCurrDefaultFormatDateStr()
 {
     String temp = convertDate2String(new Date(), "yyyyMMddHHmmss");
     return (new StringBuilder()).append("to_date('").append(temp).append("', '").append(DEFAULT_DB_DATE_FMT).append("')").toString();
 }

 public static String getDefaultTimeName(String name)
 {
     String dateStr = convertDate2String(new Date(), "yy/MM/dd HH:mm:ss");
     return (new StringBuilder("New ")).append(name).append(" ").append(dateStr).toString();
 }

 public static Date convertDateToMaxTime(Date date)
 {
     if(null == date)
         date = new Date();
     String dateString = convertDate2String(date, "yyyyMMdd");
     dateString = (new StringBuilder()).append(dateString).append("235959").toString();
     return convertString2Date(dateString, "yyyyMMddHHmmss");
 }

 private static final byte daysPerMonth[] = {
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
     30, 31
 };
 private static Map fastFormatCache;
 private static Date defaultExpireTime;
 private static final String ZEROSTRING = "000000";
 private static final String maxTimeString = "235959";
 private static final String TO_DATE = System.getProperty("TO_DATE", "TO_DATE");
 private static final String DEFAULT_DB_DATE_FMT = System.getProperty("DB_DATE_FMT", "%Y%m%d%H%M%S");

 static
 {
//     defaultExpireTime = convertString2Date(Constant.EXPIRE_TIME, "yyyyMMddHHmmss");
     fastFormatCache = new ConcurrentHashMap();
     fastFormatCache.put("yyyyMMddHHmmss", FastDateFormat.getInstance("yyyyMMddHHmmss"));
     fastFormatCache.put("yyyyMMdd", FastDateFormat.getInstance("yyyyMMdd"));
 }
}


/*
 DECOMPILATION REPORT

 Decompiled from: E:\Program Files\workspace\SIS_R002C02L10512\server\default\lib\pmp\pmp_common_src.jar
 Total time: 16 ms
 Jad reported messages/errors:
The class file version is 49.0 (only 45.3, 46.0 and 47.0 are supported)
 Exit status: 0
 Caught exceptions:
*/